package com.xoa.controller.file;

import java.io.File;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.xoa.model.file.FileAuthWrapper;
import com.xoa.model.users.Users;
import com.xoa.util.CookiesUtil;
import com.xoa.util.common.L;
import com.xoa.util.common.MobileCheck;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.common.wrapper.BaseWrappers;
import com.xoa.util.netdisk.CheckAll;
import com.xoa.util.netdisk.ZipUtils;
import com.xoa.util.treeUtil.FileCommSortTreeUtil;
import org.apache.ibatis.jdbc.Null;
import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.xoa.model.diary.DiaryModel;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.file.FileContentModel;
import com.xoa.model.file.FileSortModel;
import com.xoa.service.file.FileContentService;
import com.xoa.service.file.FileSortService;
import com.xoa.util.DateFormat;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.dataSource.ContextHolder;
import com.xoa.util.treeUtil.CheckTree;
import com.xoa.util.treeUtil.FileSortTreeUtil;
import com.xoa.util.treeUtil.HtmlUtil;
import com.xoa.util.treeUtil.TreeNode;
import org.springframework.web.servlet.ModelAndView;


/**
 * 创建作者:   杨 胜
 * 创建日期:   2017-4-18 下午4:06:47
 * 类介绍  :   文件柜模块控制层
 * 构造参数:   默认(由Spring容器托管)
 */
@Controller
@RequestMapping("/file")
public class FileController {
    private Logger loger = Logger.getLogger(FileController.class);

    @Resource
    FileSortService fileSortService;
    @Resource
    FileContentService fileContentService;

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:07:17
     * 方法介绍:   文件柜跳转首页面
     * 参数说明:   @return
     *
     * @return String
     */
    @RequestMapping(value = "/home")
    public String fileHome(HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        return "app/file/fileHome";
    }

    @RequestMapping(value = "/persionBox")
    public String persionBox(HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        return "app/file/fileHomePerson";
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:07:17
     * 方法介绍:   文件柜跳转首页面
     * 参数说明:   @return
     *
     * @return String
     */
    @RequestMapping(value = "/temp")
    public String temp(HttpServletRequest request, Integer sortId, Model model) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        model.addAttribute("sortId", sortId);
        return "app/file/temp";
    }


    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:07:34
     * 方法介绍:   文件柜顶层页面跳转
     * 参数说明:   @return
     *
     * @return String
     */
    @RequestMapping(value = "/homePerson")
    public String topFrame(HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        return "app/file/fileHomePerson";
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:09:20
     * 方法介绍:   将目录树转换成json数据通过HtmlUtil.writerJson(response, treeList)写到前台页面
     * 参数说明:   @param file
     * 参数说明:   @param response
     *
     * @return void
     */
    @RequestMapping(value = "/writeTree", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public void showFile(FileSortModel file, HttpServletResponse response, HttpSession session, HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        List<TreeNode> treeList = treeFile(file.getSortId(), request);
        HtmlUtil.writerJson(response, treeList);
    }
    @RequestMapping("/getHomeFileSortInfo")
    @ResponseBody
    public FileSortWrappers getHomeFileSortInfo(HttpServletRequest request){
        Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
        Users users = SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionId);
        String userId ="";
        if(users!=null&&users.getUid()!=null){
            userId=users.getUserId();
        }
        return fileSortService.getHomeFileSortInfo(userId);
    }


    @RequestMapping("/getAllPublicFile")
    @ResponseBody
    public BaseWrappers getAllPublicFile(){
        return fileSortService.getAllPublicFile();
    }

    @RequestMapping("/getAllPrivateFile")
    @ResponseBody
    public BaseWrappers getAllPrivateFile(HttpServletRequest request){
        Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");

        Users users = SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionId);
        String userId ="";
        if(users!=null&&users.getUid()!=null){
            userId=users.getUserId();
        }
        return fileSortService.getAllPrivateFile(userId);
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:12:07
     * 方法介绍:   构建树形目录信息
     * 参数说明:   @param sortid
     * 参数说明:   @return
     *
     * @return List<TreeNode>
     */
    public List<TreeNode> treeFile(Integer sortid,  HttpServletRequest request) {
        //Session 获取用户信息

        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users user=SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);

        String userId = user.getUserId();
        String userPriv = user.getUserPriv().toString();
        String deptId = user.getDeptId().toString();
        Map<String, Object> map = new Hashtable<String, Object>();
        map.put("userId", userId);
        map.put("userPriv", userPriv);
        map.put("deptId", deptId);
        //采用 LinkedList 双向列表实现类 操作 在链表中操作对象集合效率高
        List<FileSortModel> rootTree = new LinkedList<FileSortModel>();
        Map<String, Object> rootMap = new HashMap<String, Object>();
        if (sortid == null) sortid = 0;
        rootMap.put("sortid", sortid);
        rootMap.put("sortType", "5");
        rootTree = fileSortService.getRootTree(rootMap);// 根节点


        //利用迭代器删除集合中元素
        List<FileSortModel> childTree = new LinkedList<FileSortModel>();
        for (FileSortModel fsm : rootTree) {
            childTree.addAll(getchildTrees(fsm, map));
        }
        // 构造方法传值
        FileCommSortTreeUtil util = new FileCommSortTreeUtil(rootTree, childTree, map);
        return util.getTreeNode();
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:15:03
     * 方法介绍:   根据权限递归循环获取文件夹对象
     * 参数说明:   @param file
     * 参数说明:   @return
     *
     * @return List<FileSortModel>
     */
    public List<FileSortModel> getchildTrees(FileSortModel file, Map<String, Object> map) {
        // 传值查询子节点数据
        List<FileSortModel> parentList = new ArrayList<FileSortModel>();
        Integer tempNo = file.getSortId();
        if (tempNo == null) tempNo = 0;
        List<FileSortModel> childrenList = new ArrayList<FileSortModel>();
        childrenList = fileSortService.getSortChrildren(tempNo);
        if (childrenList.size() > 0) {
            for (FileSortModel fs : childrenList) {
                parentList.addAll(getchildTrees(fs, map));
            }
            parentList.addAll(childrenList);
        }
        return parentList;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:09:20
     * 方法介绍:   将目录树转换成json数据通过HtmlUtil.writerJson(response, treeList)写到前台页面
     * 参数说明:   @param file
     * 参数说明:   @param response
     *
     * @return void
     */
    @RequestMapping(value = "/writeTreePerson", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public void showFilePerson(FileSortModel file, HttpServletResponse response, HttpSession session, HttpServletRequest request,  @RequestParam(value = "share", required = false) String share) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        List<TreeNode> treeList = treeFilePerson(request,file.getSortId(), session,share);
        HtmlUtil.writerJson(response, treeList);
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:12:07
     * 方法介绍:   构建个人树形目录信息
     * 参数说明:   @param sortid
     * 参数说明:   @return
     *
     * @return List<TreeNode>
     */
    public List<TreeNode> treeFilePerson(HttpServletRequest request,Integer sortid, HttpSession session,
                                         @RequestParam(value = "share", required = false) String share) {
        //Session 获取用户信息
        Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");

        Users users = SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionId);
        String userId = users.getUserId();
//		String userId=session.getAttribute("userId")==null?"":session.getAttribute("userId").toString();
        //采用 LinkedList 双向列表实现类 操作 在链表中操作对象集合效率高
        List<FileSortModel> rootTree = new LinkedList<FileSortModel>();
       if(!"1".equals(share)){
           FileSortModel fs = new FileSortModel();
           fs.setUserId(userId);
           fs.setSortName("根目录");
           fs.setSortType("4");
           fs.setSortParent(-1);
           fs.setSortId(0);
           rootTree.add(fs);
       }
        if (!"".equals(userId) && userId != null) {
            FileSortModel fsm = new FileSortModel();
            if("1".equals(share)){
                fsm.setSharUser(userId);
            }else{
                fsm.setUserId(userId);
                fsm.setSortParent(0);
            }
            fsm.setSortType("4");
            List<FileSortModel> fList = fileSortService.getFileSortList(fsm);
            if (fList == null) {
                fList = new ArrayList<FileSortModel>();
            }
            rootTree.addAll(fList);// 根节点
        }
        List<FileSortModel> childTree = new LinkedList<FileSortModel>();
        // 取子节点
        for (FileSortModel fsm : rootTree) {
            if (fsm.getSortParent() != -1) {
                childTree.addAll(getchildTreePerson(fsm));
            }
        }
        //个人文件柜  根文件夹

        // 构造方法传值
        FileSortTreeUtil util = new FileSortTreeUtil(rootTree, childTree);
        return util.getTreeNode();
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-5-8 下午3:45:58
     * 方法介绍:   递归个人文件柜
     * 参数说明:   @param file
     * 参数说明:   @return
     *
     * @return List<FileSortModel>
     */
    public List<FileSortModel> getchildTreePerson(FileSortModel file) {
        // 传值查询子节点数据
        List<FileSortModel> parentList = new ArrayList<FileSortModel>();
        Integer tempNo = file.getSortId();
        List<FileSortModel> childrenList = new ArrayList<FileSortModel>();
        childrenList = fileSortService.getSortChrildren(tempNo == null ? 0 : tempNo);
        if (childrenList.size() > 0) {
            for (FileSortModel fs : childrenList) {
                parentList.addAll(getchildTreePerson(fs));
            }
            parentList.addAll(childrenList);
        }
        return parentList;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:10:52
     * 方法介绍:    验证文件柜是否重复
     * 参数说明:   @return
     *
     * @return String
     */
    @RequestMapping(value = "/getFileCheck")
    @ResponseBody
    public String getFileCheck(HttpServletRequest request, FileSortModel file) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute("loginDateSouse"));
        //只用于判断个人文件柜是否重复
        int i = 0;
        String userId = (String) request.getSession().getAttribute("userId");
        if (userId != null) {
            file.setUserId(userId);
            i = fileSortService.getFileCheck(file);
        }
        return i + "";
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:11:14
     * 方法介绍:   进入文件柜主页面
     * 参数说明:   @return
     *
     * @return String
     */
    @RequestMapping(value = "/contentAdd")
    public ModelAndView fileHomeOne(HttpServletRequest request, String sortId, String text, String contentId) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute("loginDateSouse"));
        Map<String, Object> model = new HashMap<String, Object>();
        ModelAndView modelAndView = null;
        try {
            if (StringUtils.checkNull(contentId)) {
                text = URLDecoder.decode(text, "UTF-8");
            }
            model.put("sortId", sortId);
            model.put("text", text);
            modelAndView = new ModelAndView("app/file/fileContentAdd", model);
            return modelAndView;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return modelAndView;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:11:14
     * 方法介绍:   进入文件柜主页面
     * 参数说明:   @return
     *
     * @return String
     */
    @RequestMapping(value = "/toFileDetails")
    public ModelAndView toFileDetails(HttpServletRequest request) {
        Map<String, Object> model = new HashMap<String, Object>();
        ModelAndView modelAndView = new ModelAndView("app/file/fileDetails", model);
        return modelAndView;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-6-19 下午8:55:30
     * 方法介绍:   获取文件信息
     * 参数说明:   @param request
     * 参数说明:   @param contentId
     * 参数说明:   @return
     *
     * @return FileContentModel
     */
    @RequestMapping(value = "/getContentById")
    @ResponseBody
    public FileContentModel file(HttpServletRequest request, String contentId) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute("loginDateSouse"));
        FileContentModel fcm = fileContentService.getFileConByContentId(contentId, request);
        List<Attachment> attachmentList = new ArrayList<Attachment>();
        if (fcm.getAttachmentName() != null && !"".equals(fcm.getAttachmentName())) {
            attachmentList = GetAttachmentListUtil.returnAttachment(fcm.getAttachmentName(), fcm.getAttachmentId(), "xoa" + (String) request.getSession().getAttribute("loginDateSouse"), GetAttachmentListUtil.MODULE_FILE_FOLDER);
        }
        fcm.setAttachmentList(attachmentList);
        return fcm;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-6-21 下午12:23:28
     * 方法介绍:   文件编辑
     * 参数说明:   @param request
     * 参数说明:   @param fcm
     * 参数说明:   @return
     *
     * @return ToJson
     */
    @RequestMapping(value = "/updateContent")
    @ResponseBody
    public ToJson updateContent(HttpServletRequest request, FileContentModel fcm) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute("loginDateSouse"));
        ToJson to = new ToJson();
        double filerSize = 0;
        if (!StringUtils.checkNull(fcm.getFileSize())) {
            String[] fileSizes = fcm.getFileSize().split(",");
            for (int i = 0; i < fileSizes.length; i++) {
                if (fileSizes[i].indexOf("MB") != -1) {
                    String file = fileSizes[i].replace("MB", "");
                    double size = Double.valueOf(file);
                    filerSize += size;
                } else if (fileSizes[i].indexOf("KB") != -1) {
                    String file = fileSizes[i].replace("KB", "");
                    double size = Double.valueOf(file);
                    double sizeFile = ZipUtils.getM(size);
                    BigDecimal b = new BigDecimal(sizeFile);
                    double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    filerSize += f1;
                } else if (fileSizes[i].indexOf("B") != -1) {
                    String file = fileSizes[i].replace("B", "");
                    double size = Double.valueOf(file);
                    double sizeFile = ZipUtils.getKB(size);
                    double sizeFile1 = ZipUtils.getM(sizeFile);
                    BigDecimal b = new BigDecimal(sizeFile1);
                    double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    filerSize += f1;
                }
            }

        }
        fcm.setFileSize(String.valueOf(filerSize));
        if(fcm.getSortId()!=0){
            FileSortModel file=new FileSortModel();
        file.setSortId(fcm.getSortId());
        FileSortModel fileSortModel= fileSortService.getFileSortBySortIds(file);
        if ("5".equals(fileSortModel.getSortType())){
            StringBuffer stringBuffer=new StringBuffer();

            List<FileSortModel>   FileSortModels  =  getfilesDeleteList(file);

            FileSortModels.add(fileSortModel);
         //   List<TreeNode> trr = treeFile(fcm.getSortId(), request.getSession());
            for(FileSortModel treeNode:FileSortModels){
                stringBuffer.append(treeNode.getSortId());
                if(FileSortModels.size()>1){
                    stringBuffer.append(",");
                }

            }
            List<FileContentModel> fileContentModels=new ArrayList<FileContentModel>();
                 String sortId="";
                if(!StringUtils.checkNull(stringBuffer.toString())){
                    if(FileSortModels.size()>1) {
                        sortId = stringBuffer.substring(0, stringBuffer.length() - 1);
                    }else {
                        sortId=stringBuffer.toString();
                    }
                    fileContentModels =  fileContentService.selectFileSortList(sortId);
                }
            double fileListSize=0;
            for(int i=0;i<fileContentModels.size();i++){
                fileListSize+=Double.parseDouble(fileContentModels.get(i).getFileSize());
            }
            BaseWrapper bassWrapper = fileSortService.getFileAuthBySortId(fcm.getSortId(), request);//不想在写个sql了 用个已有的
            if(bassWrapper!=null){
                if( bassWrapper.getData()!=null&&bassWrapper.getData() instanceof FileSortModel){
                    FileSortModel model  =(FileSortModel) bassWrapper.getData();
                    if(StringUtils.checkNull(String.valueOf(model.getSpaceLimit()))){
                    }else {
                        double size=Double.valueOf(model.getSpaceLimit());
                        if(fileListSize+filerSize<size||size==0.0){

                        }else {
                            to.setFlag(1);
                            to.setMsg("文件已经到最大容量");
                            return  to;
                        }
                    }
                }
            }

        }
    }
        int i = fileContentService.updateFileCon(fcm);

        to.setFlag(0);
        to.setMsg(i + "");
        return to;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:11:45
     * 方法介绍:   文件展示页面跳转
     * 参数说明:   @return
     *
     * @return String
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/content")
    public ModelAndView fileContent(String sortType, String sortId, String text, String postType, HttpServletRequest request) throws UnsupportedEncodingException {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("sortId", sortId);
        model.put("sortType", sortType);
        model.put("text", text);
        model.put("postType", postType);
        ModelAndView modelAndView = new ModelAndView("app/file/fileContent", model);
        return modelAndView;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-26 上午9:06:06
     * 方法介绍:   新建文件
     * 参数说明:   @return
     *
     * @return String
     */
    @RequestMapping(value = "saveContent")
    @ResponseBody
    public ToJson<DiaryModel> addContent(FileContentModel fileContentModel, HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        ToJson diaryListToJson = new ToJson(0, "");
        double filerSize = 0;
        if (!StringUtils.checkNull(fileContentModel.getFileSize())) {
            String[] fileSizes = fileContentModel.getFileSize().split(",");
            for (int i = 0; i < fileSizes.length; i++) {
                if (fileSizes[i].indexOf("MB") != -1) {
                    String file = fileSizes[i].replace("MB", "");
                    double size = Double.valueOf(file);
                    filerSize += size;
                } else if (fileSizes[i].indexOf("KB") != -1) {
                    String file = fileSizes[i].replace("KB", "");
                    double size = Double.valueOf(file);
                    double sizeFile = ZipUtils.getM(size);
                    BigDecimal b = new BigDecimal(sizeFile);
                    double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    filerSize += f1;
                } else if (fileSizes[i].indexOf("B") != -1) {
                    String file = fileSizes[i].replace("B", "");
                    double size = Double.valueOf(file);
                    double sizeFile = ZipUtils.getKB(size);
                    double sizeFile1 = ZipUtils.getM(sizeFile);
                    BigDecimal b = new BigDecimal(sizeFile1);
                    double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    filerSize += f1;
                }
            }

        }
        fileContentModel.setFileSize(String.valueOf(filerSize));
        if(fileContentModel.getSortId()!=0){
            FileSortModel file=new FileSortModel();
        file.setSortId(fileContentModel.getSortId());
        FileSortModel fileSortModel= fileSortService.getFileSortBySortIds(file);
        if ("5".equals(fileSortModel.getSortType())){
            StringBuffer stringBuffer=new StringBuffer();

            List<FileSortModel>   FileSortModels  =  getfilesDeleteList(file);
            FileSortModels.add(fileSortModel);
          //  List<TreeNode> trr = treeFile(fileContentModel.getSortId(), request.getSession());
            for(FileSortModel treeNode:FileSortModels){
                stringBuffer.append(treeNode.getSortId());
                if(FileSortModels.size()>1){
                    stringBuffer.append(",");
                }
            }
            List<FileContentModel> fileContentModels=new ArrayList<FileContentModel>();
            String sortId="";
            if(!StringUtils.checkNull(stringBuffer.toString())){
                if(FileSortModels.size()>1) {
                    sortId = stringBuffer.substring(0, stringBuffer.length() - 1);
                }else {
                    sortId=stringBuffer.toString();
                }
                fileContentModels = fileContentService.selectFileSortList(sortId);
            }
            double fileListSize=0;
            for(int i=0;i<fileContentModels.size();i++){
                fileListSize+=Double.parseDouble(fileContentModels.get(i).getFileSize());
            }
            BaseWrapper bassWrapper = fileSortService.getFileAuthBySortId(fileContentModel.getSortId(), request);//不想在写个sql了 用个已有的
            if(bassWrapper!=null){
                 if( bassWrapper.getData()!=null&&bassWrapper.getData() instanceof FileSortModel){
                     FileSortModel model  =(FileSortModel) bassWrapper.getData();
                     if(StringUtils.checkNull(String.valueOf(model.getSpaceLimit()))){
                      }else {
                         double size=Double.valueOf(model.getSpaceLimit());
                         if(fileListSize+filerSize<size||size==0.0){

                         }else {
                             diaryListToJson.setFlag(1);
                             diaryListToJson.setMsg("文件已经到最大容量");
                             return  diaryListToJson;
                         }
                     }
                 }
            }



        }
        }
        fileContentModel.setSendTime(DateFormat.getStrDate(new Date()));

       String userId="";
        Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");

        Users users = SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionId);
        if(users!=null&&users.getUid()!=null){
            userId=  users.getUserId();
        }
        fileContentModel.setCreater(userId);
        if (fileContentModel.getSortId() == 0) {
            fileContentModel.setUserId(userId);
        }
        int i = fileContentService.saveContent(fileContentModel);
        diaryListToJson.setFlag(0);
        diaryListToJson.setMsg("" + i);
        return diaryListToJson;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-26 上午9:06:53
     * 方法介绍:   查看文件内容
     * 参数说明:   @param contentId
     * 参数说明:   @return
     *
     * @return ModelAndView
     */
    @RequestMapping(value = "/catContent")
    public ModelAndView catContent(String contentId, HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        loger.info("--------catContent-------");
        Map<String, Object> model = new HashMap<String, Object>();
        FileContentModel fcm = fileContentService.getFileConByContentId(contentId, request);
        model.put("fcm", fcm);
        ModelAndView modelAndView = new ModelAndView("app/file/fileContentDetail", model);
        return modelAndView;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:12:25
     * 方法介绍:   通过循环获取条判断返回不同类型数据 从而一个请求接口处理多种业务
     * 参数说明:   @param file
     * 参数说明:   @return
     *
     * @return void  通过HtmlUtil.writerJson() 方法中out对象写到response中返回json数据
     */
    @RequestMapping(value = "/catalog")
    public void showFiles(FileSortModel file, HttpServletResponse response, String postType, HttpServletRequest request, String deptId, String userPriv) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute("loginDateSouse"));
        // "redirect:/showFile" "file/showFile"
        List<FileSortModel> fileList = new LinkedList<FileSortModel>();
        List<Object> tatalList = new LinkedList<Object>();
        List<FileContentModel> fileConList = new LinkedList<FileContentModel>();
        Map<String, Object> map = new Hashtable<String, Object>();
        String userId = request.getSession().getAttribute("userId").toString();
        if (userPriv == null) {
            userPriv = request.getSession().getAttribute("userPriv").toString();
        }
        if (deptId == null) {
            deptId = request.getSession().getAttribute("deptId").toString();
        }
        if (file.getUserId() == null) {
            file.setUserId(userId);
        }
        map.put("userId", file.getUserId() == null ? userId : file.getUserId());
        map.put("userPriv", userPriv == null ? "" : userPriv);
        map.put("deptId", deptId == null ? "" : deptId);
        Integer tempNo = file.getSortId();
        //获取文件
        if (tempNo == null && "5".equals(file.getSortType())) {

        } else {
            fileConList = fileContentService.getFileConBySortid(tempNo == null ? 0 : tempNo, request);
        }
        for (FileContentModel fcm : fileConList) {
            List<Attachment> attachmentList = new ArrayList<Attachment>();
            if (fcm.getAttachmentName() != null && !"".equals(fcm.getAttachmentName())) {
                attachmentList = GetAttachmentListUtil.returnAttachment(fcm.getAttachmentName(), fcm.getAttachmentId(), "xoa" + (String) request.getSession().getAttribute("loginDateSouse"), GetAttachmentListUtil.MODULE_FILE_FOLDER);
            }
            fcm.setAttachmentList(attachmentList);
        }
        //通过判断获取父文件夹或子文件夹
        if (file.getSortId() == null) {
            FileSortModel fileChr = new FileSortModel();
            fileChr.setSortParent(0);
            fileChr.setSortType(file.getSortType());
            //公共文件柜  类型为5
            if ("5".equals(fileChr.getSortType())) {
                fileList = fileSortService.getFileSortList(fileChr);
                //利用迭代器删除集合中元素
                if (!MobileCheck.isMobileDevice(request.getHeader("user-agent"))) {
                    Iterator<FileSortModel> iteratorChr = fileList.iterator();
                    while (iteratorChr.hasNext()) {
                        FileSortModel fsm = iteratorChr.next();
                        //将权限字符串传到checkAll 返回为true时 有权限 为false时无权限
                        if (!CheckTree.checkAll(fsm.getUserId(), map)) {
                            iteratorChr.remove();
                        }
                    }
                }

            }
            //个人文件柜  类型为4
            if ("4".equals(fileChr.getSortType())) {
                fileChr.setUserId(file.getUserId());
                fileList = fileSortService.getFileSortList(fileChr);
            }
        } else {
            FileSortModel filePar = new FileSortModel();
            filePar.setSortParent(file.getSortId());
            filePar.setSortType(file.getSortType());
            fileList = fileSortService.getFileSortList(filePar);
            //postType为1为pc端请求，2为app端    app端控制权限
            if ("5".equals(file.getSortType())) {
                if (!MobileCheck.isMobileDevice(request.getHeader("user-agent"))) {
                    Iterator<FileSortModel> iteratorChr = fileList.iterator();
                    while (iteratorChr.hasNext()) {
                        FileSortModel fsm = iteratorChr.next();
                        //将权限字符串传到checkAll 返回为true时 有权限 为false时无权限
                        if (!CheckTree.checkAll(fsm.getUserId(), map)) {
                            iteratorChr.remove();
                        }
                    }
                }

            }
            //个人文件柜  类型为4
            if ("4".equals(file.getSortType())) {
                filePar.setUserId(userId);
                fileList = fileSortService.getFileSortList(filePar);
            }
        }

        if (fileList != null) {
            checkPriv(fileList, map);
            tatalList.addAll(fileList);
        }
        if (fileConList != null) {

            tatalList.addAll(fileConList);
        }
        HtmlUtil.writerJson(response, tatalList);
    }

    private void checkPriv(List<FileSortModel> fileList, Map<String, Object> map) {
        for (FileSortModel m : fileList) {
            m.setDelPriv(CheckAll.checkAll(m.getDelUser(), map));
            m.setMakePriv(CheckAll.checkAll(m.getNewUser(), map));
            m.setDownloadPriv(CheckAll.checkAll(m.getDownUser(), map));
            m.setUpdatePriv(CheckAll.checkAll(m.getManagerUser(), map));
        }
    }


    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:12:47
     * 方法介绍:   文件柜设置跳转页面
     * 参数说明:   @param file
     * 参数说明:   @return
     *
     * @return ModelAndView
     */
    @RequestMapping("/setIndex")
    public ModelAndView tofileSet() {
        ModelAndView modelAndView = new ModelAndView("app/file/fileSet");
        return modelAndView;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-7-5 上午10:35:27
     * 方法介绍:   签阅情况跳转页面
     * 参数说明:   @return
     *
     * @return ModelAndView
     */
    @RequestMapping("/signReading")
    public ModelAndView signReading() {
        ModelAndView modelAndView = new ModelAndView("app/file/signReading");
        return modelAndView;
    }


    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:12:47
     * 方法介绍:   获取根文件夹
     * 参数说明:   @param file
     * 参数说明:   @return
     *
     * @return ModelAndView
     */
    @RequestMapping("/toFileSet")
    @ResponseBody
    public List<FileSortModel> showFileBySort_id(FileSortModel file, HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        // "redirect:/showFile" "file/showFile"
        HttpSession session = request.getSession();
        String userId = session.getAttribute("userId").toString();
        String userPriv = session.getAttribute("userPriv").toString();
        String deptId = session.getAttribute("deptId").toString();
        Map<String, Object> map = new Hashtable<String, Object>();
        map.put("userId", userId == null ? "" : userId);
        map.put("userPriv", userPriv == null ? "" : userPriv);
        map.put("deptId", deptId == null ? "" : deptId);
        file.setSortType("5");
        file.setSortParent(0);
        List<FileSortModel> list = fileSortService.getFileSortList(file);
//		Iterator<FileSortModel> iteratorChr=list.iterator();
//		 while(iteratorChr.hasNext()){
//					 FileSortModel fsm=iteratorChr.next();
//					//将权限字符串传到checkAll 返回为true时 有权限 为false时无权限
//					if(!this.checkAll(fsm.getUserId(),map)){
//						iteratorChr.remove();
//			      }
//		   }
        checkPriv(list, map);
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("parentList", list);
        return list;
    }


    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:13:10
     * 方法介绍:   添加文件夹，通过判断字段，重复使用//添加子文件夹及父文件夹通用
     * 参数说明:   @throws UnsupportedEncodingException
     *
     * @return ModelAndView
     */
    @ResponseBody
    @RequestMapping("/add")
    public ToJson<FileSortModel> fileAdd(FileSortModel file, HttpServletRequest request, HttpServletResponse response)
            throws UnsupportedEncodingException {
        ToJson<FileSortModel> toJson = new ToJson<>(1, "err");
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        HttpSession session = request.getSession();
        String userId = session.getAttribute("userId").toString();
        Map<String, Object> model = null;
        ModelAndView modelAndView = null;
    /*	if("".equals(file.getSortNo())||file.getSortNo()==null){
			
		if ("".equals(file.getSortName())||file.getSortName()==null) {
			modelAndView = new ModelAndView("app/file/fileAdd", null);
			return modelAndView;
		}
		
		if (!"".equals(file.getSortName())||file.getSortName()!=null) {
			modelAndView = new ModelAndView("app/file/addFile", null);
			return modelAndView;
		}
		}*/
        // 乱码处理-----开始
        //FIXME by pfl 修改乱码问题
        String sname = new String(file.getSortName());
        file.setSortName(sname);
        // 乱码处理-----结束
        //新建子文件夹
        if (file.getSortId() != null) {
            file.setSortParent(file.getSortId());
            file.setSortId(0);
            int resultSaveChr = fileSortService.saveFileSortChr(file);
            HtmlUtil.writerJson(response, resultSaveChr);
            return null;
        }

        //添加文件影响行
        if ("5".equals(file.getSortType())) {
            file.setUserId("||" + userId + ",");
        } else {
            file.setUserId(userId);
        }
        if (file.getSortParent() == null) {
            file.setSortParent(0);
        }
        if (file.getSortNo() != null) {
            FileSortModel file1 = fileSortService.singleBySortName(file.getSortName());
            if (file1 != null) {
                toJson.setMsg("文件名不能重复");
                file.setCount(1);
                toJson.setFlag(1);
                toJson.setCode("1001");
            } else {
                int resultSave = fileSortService.saveFileSort(file);
                HtmlUtil.writerJson(response, resultSave);
            }
        }
        return toJson;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:13:36
     * 方法介绍:   获取一个文件夹信息
     * 参数说明:   @param file
     * 参数说明:   @return
     *
     * @return ModelAndView
     */
    @RequestMapping("/fileGetOne")
    @ResponseBody
    public ToJson<FileSortModel> filegetOne(FileSortModel file, HttpServletRequest request) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute("loginDateSouse"));
        ToJson<FileSortModel> json = new ToJson<FileSortModel>();
        FileSortModel fs = fileSortService.getFileSortBySortIds(file);
        json.setObject(fs);
        return json;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:14:01
     * 方法介绍:   编辑文件夹信息传值跳转
     * 参数说明:   @param file
     * 参数说明:   @return
     *
     * @return ModelAndView
     */
    @RequestMapping("/edit")
    public ModelAndView fileEdit(FileSortModel file, HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        // "redirect:/showFile" "file/showFile"
        Map<String, Object> fileEditMap = new HashMap<String, Object>();
        List<FileSortModel> fileslist = fileSortService.getFileSortList(file);
        FileSortModel files = null;
        if (fileslist.size() > 0) {
            files = fileslist.get(0);
        }
        fileEditMap.put("sortid", files.getSortId());
        fileEditMap.put("sortno", files.getSortNo());
        fileEditMap.put("sortname", files.getSortName());
        ModelAndView modelAndView = new ModelAndView("app/file/fileEdit", fileEditMap);
        return modelAndView;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:14:19
     * 方法介绍:   修改文件夹信息
     * 参数说明:   @param file
     * 参数说明:   @return
     *
     * @return ModelAndView
     */
    @RequestMapping("/update")
    public void fileUpdate(FileSortModel file, HttpServletRequest request, HttpServletResponse response) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        //修改文件影响行
        int resultUpdate = fileSortService.updateFileSort(file);
        HtmlUtil.writerJson(response, resultUpdate);
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:14:39
     * 方法介绍:   删除目录
     * 参数说明:   @param request
     * 参数说明:   @param file
     * 参数说明:   @return
     *
     * @return ModelAndView
     */
    @RequestMapping("/deleteAll")
    @ResponseBody
    public ToJson fileDelete(HttpServletRequest request, FileSortModel file) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        // 所有删除文件夹
        List<FileSortModel> childrenList = getfilesDeleteList(file);
        // 将父节点加入，父节点下可能也有文件
        childrenList.add(file);
        // 文件集合
        List<FileContentModel> fileContentList = new ArrayList<FileContentModel>();
        //
        for (FileSortModel f : childrenList) {
            int tempNo = f.getSortId();
            List<FileContentModel> fileContent = fileContentService
                    .getFileConBySortid(tempNo, request);
            fileContentList.addAll(fileContent);
            //删除文件影响行
            int deleConNo = fileContentService
                    .deleteBySortId(tempNo);
        }
        // 删除附件
        // boolean flag=deleteAttachment(fileContentList,request);
        // 删除父节点，子节点
        int deleSortNo = 0;
        for (FileSortModel f : childrenList) {
            Map<String, Object> fileSortidMap = new HashMap<String, Object>();
            fileSortidMap.put("sortid", f.getSortId());
            //删除文件影响行
            deleSortNo += fileSortService
                    .deleteBySortId(fileSortidMap);
        }
        ToJson tj = new ToJson();
        if (deleSortNo > 0) {
            tj.setFlag(0);
            tj.setMsg("删除成功");
        }
        return tj;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:14:39
     * 方法介绍:   删除文件
     * 参数说明:   @param request
     * 参数说明:   @param file
     * 参数说明:   @return
     *
     * @return ModelAndView
     */
    @RequestMapping("/deletefileAndCon")
    @ResponseBody
    public ToJson conDelete(HttpServletRequest request, FileSortModel file, String contentId) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        int deleSortNo = 0;
        ToJson tj = new ToJson();
        // 所有删除文件夹
        if (contentId == null) {
            /*List<FileSortModel> childrenList = getfilesDeleteList(file);
            // 将父节点加入，父节点下可能也有文件
            childrenList.add(file);
            // 文件集合
            for (FileSortModel f : childrenList) {
                int tempNo = f.getSortId();
                int deleConNo = fileContentService
                        .deleteBySortId(tempNo);
            }

            // 删除附件
            // boolean flag=deleteAttachment(fileContentList,request);
            // 删除父节点，子节点
            for (FileSortModel f : childrenList) {
                Map<String, Object> fileSortidMap = new HashMap<String, Object>();
                fileSortidMap.put("sortid", f.getSortId());
                //删除文件影响行
                deleSortNo += fileSortService
                        .deleteBySortId(fileSortidMap);
            }*/
            tj.setFlag(0);
            tj.setMsg(deleSortNo + "");
        } else {
            Map<String, Object> fileConMap = new HashMap<String, Object>();
            fileConMap.put("contentId", contentId);
            //deleSortNo = fileContentService.deleteByConId(fileConMap);
            tj.setFlag(0);
            tj.setMsg(deleSortNo + "");
        }
        return tj;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:15:03
     * 方法介绍:   递归循环获取文件夹对象
     * 参数说明:   @param file
     * 参数说明:   @return
     *
     * @return List<FileSortModel>
     */
    public List<FileSortModel> getfilesDeleteList(FileSortModel file) {
        // 传值查询子节点数据
        List<FileSortModel> parentList = new ArrayList<FileSortModel>();
        Integer tempNo = file.getSortId();
        List<FileSortModel> childrenList = new ArrayList<FileSortModel>();
        childrenList = fileSortService.getSortChrildren(tempNo);
        if (childrenList.size() > 0) {
            for (FileSortModel fs : childrenList) {
                parentList.addAll(getfilesDeleteList(fs));
            }
            parentList.addAll(childrenList);
        }
        return parentList;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-4-18 下午4:15:18
     * 方法介绍:   删除附件
     * 参数说明:   @param fileContentList
     * 参数说明:   @param request
     * 参数说明:   @return
     *
     * @return boolean
     */
    private boolean deleteAttachment(List<FileContentModel> fileContentList, HttpServletRequest request) {
        List<Attachment> attachmentList = new ArrayList<Attachment>();
        // 遍历处理附件字段进行查询附件操作
        for (FileContentModel fc : fileContentList) {
            String[] Att_idArray = fc.getAttachmentId().split(",");
            for (int i = 0; i < Att_idArray.length; i++) {
                String[] Att_idArrayAid = Att_idArray[i].split("@");
                Attachment att = new Attachment();
                att.setAid(Integer.parseInt(Att_idArrayAid[0]));
                String[] ymAndAttachId = Att_idArrayAid[1].split("_");
                att.setYm(ymAndAttachId[0]);
                att.setAttachId(ymAndAttachId[1]);
                attachmentList.add(att);
            }
        }
        for (Attachment at : attachmentList) {
            // 定义"条件map集合"确定一条删除的记录
            Map<String, Object> atMap = new HashMap<String, Object>();
            atMap.put("aidString", at.getAid());
            atMap.put("ymString", at.getYm());
            atMap.put("attachIdString", at.getAttachId());
            // 删除的记录attachment
            // Attachment attachment=enclosureService.getByatMap(atMap);
            String path = request.getSession().getServletContext()
                    .getRealPath("/");
        }
        return false;
    }

    /**
     * 创建作者:   韩东堂
     * 创建日期:   2017-5-16 上午9:57:41
     * 方法介绍:   批量删除
     *
     * @param fileId 批量删除Id串
     * @return
     */
    @RequestMapping("/batchDeleteConId")
    @ResponseBody
    public BaseWrapper deleteByList(@RequestParam(name = "fileId[]") Integer fileId[]) {
        return fileContentService.batchDeleteConId(fileId);
    }


    /**
     * 创建作者:   韩东堂
     * 创建日期:   2017-6-02 上午10:57:31
     * 方法介绍:   文件柜权限修改
     *
     * @param auth 批量删除Id串
     *             数据格式：转化成json            var  data={
     *             userId:
     *             {
     *             dept:'1,2,4,5,7,',
     *             user:'admin,lilie',
     *             role:"12,3,4,5,6",
     *             },
     *             manageUser:
     *             {
     *             dept:'1,2,4,5,8,',
     *             user:'admin,lilie',
     *             role:"12,3,4,5,6",
     *             },
     *             delUser:
     *             {
     *             dept:'1,2,4,5,8,',
     *             user:'admin,lilie',
     *             role:"12,3,4,5,6",
     *             },
     *             downUser:{
     *             dept:'1,2,4,5,8,',
     *             user:'admin,lilie',
     *             role:"12,3,4,5,6",
     *             },
     *             shareUser:{
     *             dept:'1,2,4,5,8,',
     *             user:'admin,lilie',
     *             role:"12,3,4,5,6",
     *             },
     *             owner:{
     *             dept:'1,2,4,5,8,',
     *             user:'admin,lilie',
     *             role:"12,3,4,5,6",
     *             },
     *             signUser:{
     *             dept:'1,2,4,5,8,',
     *             user:'admin,lilie',
     *             role:"12,3,4,5,6",
     *             },
     *             review:{
     *             dept:'1,2,4,5,8,',
     *             user:'admin,lilie',
     *             role:"12,3,4,5,6",
     *             },
     *             description:{
     *             dept:'1,2,4,5,8,',
     *             user:'admin,lilie',
     *             role:"12,3,4,5,6",
     *             },
     *             newUser{
     *             dept:'1,2,4,5,8,',
     *             user:'admin,lilie',
     *             role:"12,3,4,5,6",
     *             },
     *             sortId:49,
     *             }
     * @return
     */
    @RequestMapping("/setFileSortAuth")
    @ResponseBody
    public BaseWrapper setFileAuth(@RequestParam(name = "auth") String auth
//								   @RequestParam(name ="userId",required = false) Map<String,Object> userId,
//								   @RequestParam(name ="manageUser",required = false) Map<String,Object> manageUser,
//								   @RequestParam(name ="delUser",required = false) Map<String,Object> delUser,
//								   @RequestParam(name ="downUser",required = false) Map<String,Object> downUser,
//								   @RequestParam(name ="shareUser",required = false) Map<String,Object> shareUser,
//								   @RequestParam(name ="owner",required = false) Map<String,Object> owner,
//								   @RequestParam(name ="signUser",required = false) Map<String,Object> signUser,
//								   @RequestParam(name ="review",required = false) Map<String,Object> review,
//								   @RequestParam(name ="description",required = false) Map<String,Object> description
    ) {
        Map<String, Object> mmp = JSONArray.parseObject(auth, Map.class);
        L.w(mmp);
        BaseWrapper wrapper = fileSortService.setFileSortAuth(mmp);
        return wrapper;
    }

    /**
     * 创建作者:   韩东堂
     * 创建日期:   2017-6-05 下午15:27:41
     * 方法介绍:   公共文件柜查询接口
     *
     * @param sortId         所属分类
     * @param subjectName    文件名称
     * @param creater        创建者名字串
     * @param contentNo      排序Id
     * @param contentValue1  关键字1
     * @param contentValue2  关键字2
     * @param contentValue3  关键字3
     * @param atiachmentDesc 附件描述
     * @param atiachmentName 附件名称
     * @param atiachmentCont 附件内容
     * @param crStartDate    创建时间范围 开始 格式 “yyyy-MM-dd HH:mm:ss”
     * @param crEndDate      创建时间范围 结束 格式 “yyyy-MM-dd HH:mm:ss”
     * @param pageNo         翻页==当前页
     * @param pageSize       翻页==每页个数
     * @return
     */
    @RequestMapping("/queryBySearchValue")
    @ResponseBody
    public BaseWrappers queryBySearchValue(HttpServletRequest req, Integer sortId, Integer sortType, String subjectName, @RequestParam(name = "creater", required = false) String creater[],
                                           Integer contentNo, String contentValue1, String contentValue2, String contentValue3,
                                           String atiachmentDesc, String atiachmentName, String atiachmentCont, String crStartDate, String crEndDate, Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        BaseWrappers wrappers = fileContentService.queryBySearchValue(req, sortId, sortType, subjectName, creater, contentNo, contentValue1, contentValue2, contentValue3, atiachmentDesc, atiachmentName, atiachmentCont, crStartDate, crEndDate, pageNo, pageSize);
        return wrappers;
    }


    /**
     * 创建作者:   韩东堂
     * 创建日期:   2017-6-06 14:27:41
     * 方法介绍:   公共文件柜权限会显接口
     *
     * @param sortId 所属分类
     */
    @RequestMapping("/getAuthBySortId")
    @ResponseBody
    public FileAuthWrapper getAuthBySortId(Integer sortId) {
        return fileSortService.getAuthBySortId(sortId);
    }

    @RequestMapping("/getFileAuthBySortId")
    @ResponseBody
    public BaseWrapper getFileAuthBySortId(Integer sortId, HttpServletRequest request) {
        return fileSortService.getFileAuthBySortId(sortId, request);
    }

    /**
     * 创建作者:   韩东堂
     * 创建日期:   2017-6-07 20:27:41
     * 方法介绍:   公共文件柜复制粘贴功能
     */
    @RequestMapping("/copyAndParse")
    @ResponseBody
    public BaseWrapper copyAndParse(Integer copyId, Integer witchSortId) {
        return fileContentService.copyAndParse(copyId, witchSortId);
    }

    /**
     * 创建作者:   杨胜
     * 创建日期:   2017-6-07 20:27:41
     * 方法介绍:   公共文件柜剪切功能
     */
    @RequestMapping("/fileCut")
    @ResponseBody
    public ToJson ContentCut(HttpServletRequest req, @RequestParam("contentId[]") String[] contentId) {
        //,@RequestBody String[] contentId
        return fileContentService.contentCut(req, contentId);
    }

    /**
     * 创建作者:   韩东堂
     * 创建日期:   2017-6-08 19:57:41
     * 方法介绍:   公共文件柜上传
     */
    @RequestMapping("/fileBoxUpload")
    @ResponseBody
    public void fileBoxUpload(@RequestParam("file") MultipartFile[] files, HttpServletRequest request, HttpServletResponse response) {
        try {
            BaseWrappers wrappers = fileContentService.uploadFile(files, request);
            response.setCharacterEncoding("utf-8");
            response.setHeader("content-type", "text/html;charset=utf-8");
            ServletOutputStream out = response.getOutputStream();
            OutputStreamWriter ow = new OutputStreamWriter(out, "UTF-8");

            ow.write(JSONObject.toJSONString(wrappers));
            ow.flush();
            ow.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return;
    }

    @Value("${file_box_new_file}")
    String file_box_new_file;

    /**
     * 创建作者:   韩东堂
     * 创建日期:   2017-6-13 19:57:41
     * 方法介绍:   公共文件柜新建文件
     */
    @RequestMapping("/newFileInBox")
    @ResponseBody
    public BaseWrapper newFileInBox(String fileType) {
        return fileContentService.newFileInBox(fileType, file_box_new_file);
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-6-19 上午9:36:46
     * 方法介绍:   打包下载文件
     * 参数说明:   @param request
     * 参数说明:   @param response
     * 参数说明:   @param cIds
     * 参数说明:   @return
     *
     * @return ToJson<?>
     */
    @RequestMapping(value = "/downFileContent", produces = {"application/json;charset=UTF-8"})
    public ModelAndView downFileContent(HttpServletRequest request, HttpServletResponse response, @RequestParam("contentId") String[] cIds) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute("loginDateSouse"));
        ToJson<?> tojson = new ToJson();
        tojson = fileContentService.downFileContent(request, response, cIds);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msg", tojson.getMsg());
        ModelAndView mod = new ModelAndView("app/common/FileDownAlert", map);
        return mod;
    }


    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-6-19 上午9:36:46
     * 方法介绍:   全局搜索
     * 参数说明:   @param request
     * 参数说明:   @param response
     * 参数说明:   @param cIds
     * 参数说明:   @return
     *
     * @return ToJson<?>
     */
    @RequestMapping("/serachAll")
    @ResponseBody
    public ToJson serachAll(HttpServletRequest request, HttpServletResponse response) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute("loginDateSouse"));
        ToJson tojson = new ToJson();
        Map<String, String> mapParamter = new HashMap<String, String>();
        Map<String, String[]> paramMap = request.getParameterMap();

        Iterator<String> iKeys = paramMap.keySet().iterator();
        while (iKeys.hasNext()) {
            String key = iKeys.next();
            String[] value = paramMap.get(key);
            if (value != null && value.length > 0) {
                mapParamter.put(key, value[0]);
            } else {
                mapParamter.put(key, "");
            }
        }
        tojson = fileContentService.serachAll(mapParamter, request);
        return tojson;
    }

    /**
     * 创建作者:   杨 胜
     * 创建日期:   2017-7-5 上午10:14:15
     * 方法介绍:   签阅情况
     * 参数说明:   @param file
     * 参数说明:   @param request
     * 参数说明:   @return
     *
     * @return List<FileSortModel>
     */
    @RequestMapping("/signConState")
    @ResponseBody
    public ToJson signConState(HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        ToJson tojson = new ToJson();
        Map<String, String> mapParamter = new HashMap<String, String>();
        Map<String, String[]> paramMap = request.getParameterMap();

        Iterator<String> iKeys = paramMap.keySet().iterator();
        while (iKeys.hasNext()) {
            String key = iKeys.next();
            String[] value = paramMap.get(key);
            if (value != null && value.length > 0) {
                mapParamter.put(key, value[0]);
            } else {
                mapParamter.put(key, "");
            }
        }
        tojson = fileContentService.signConState(mapParamter, request);
        return tojson;
    }

    //H5微应用
    //个人文件柜首页
    @RequestMapping("/fileIndexh5")
    public String fileIndex() {
        return "/app/file/m/indexh5";
    }
    //个人文件柜详情页
    @RequestMapping("/fileContenth5")
    public String fileContent() {
        return "/app/file/m/file_contenth5";
    }
    //公共文件柜首页
    @RequestMapping("/publicIndexh5")
    public String publicIndex() {
        return "/app/file/m/public_indexh5";
    }
    //公共文件柜详情页
    @RequestMapping("/publicContenth5")
    public String publicContent() {
        return "/app/file/m/public_contenth5";
    }

    /**
     * 个人文件柜共享和取消共享
     * @param fileSortModel
     * @return
     */
    @RequestMapping("/updateSingPriv")
    @ResponseBody
    public ToJson<FileSortModel> updateSingPriv(FileSortModel fileSortModel) {
       return fileSortService.updateSingPriv(fileSortModel);
    }



}
